package com.fingard.dsp.bank.directbank.gzelink01;

import java.util.HashMap;

import com.alibaba.fastjson.JSONObject;
import com.fingard.dsp.bank.directConfig.ActSetItem;
import com.fingard.dsp.bank.directbank.bankInterface.IOtherElse;
import com.fingard.dsp.bank.reqrespobj.otherelse.ReqOtherElse;
import com.fingard.dsp.bank.reqrespobj.otherelse.RetAtsOtherElse;

public class GZELINK01OtherElse extends GZELINK01Base implements IOtherElse {
	public RetAtsOtherElse d_else(ReqOtherElse p_reqElse) throws Exception {
		String transType = p_reqElse.reqHeader.transType;
		
		if ("VerifySign".equals(transType)) {
			// 网关支付验签
			return verifyGZELINK(p_reqElse);
		} else {
			RetAtsOtherElse retAts = new RetAtsOtherElse(p_reqElse.reqHeader);
			
			retAts.retHashMap = new HashMap<String, String>();
			retAts.retHashMap.put("RespCode", "-1");
			retAts.retHashMap.put("RespInfo", "未知的TransType：" + transType);
			
			return retAts;
		}
	}
	
	public RetAtsOtherElse verifyGZELINK(ReqOtherElse p_reqElse) {
		RetAtsOtherElse retAts = new RetAtsOtherElse(p_reqElse.reqHeader);

		retAts.retHashMap = new HashMap<String, String>();
		try {
			String ownAct = "";
			if (p_reqElse.reqHashMap.containsKey("OurBankAccount")) {
				ownAct = p_reqElse.reqHashMap.get("OurBankAccount");
			}
			
			String merchantId = "";
			if (p_reqElse.reqHashMap.containsKey("MchId")) {
				merchantId = p_reqElse.reqHashMap.get("MchId");
			}
			
			String appId = "";
			if (p_reqElse.reqHashMap.containsKey("AppId")) {
				appId = p_reqElse.reqHashMap.get("AppId");
			}
			
			String plain = "";
			if (p_reqElse.reqHashMap.containsKey("Plain")) {
				plain = p_reqElse.reqHashMap.get("Plain");
			} else {
				retAts.retHashMap.put("RespCode", "-1");
				retAts.retHashMap.put("RespInfo", "缺失验签原始串");

				return retAts;
			}

			ActSetItem actItem = getBankConfig().getActSetItemByAny(ownAct, merchantId, appId, "");
			if (actItem != null) {
				JSONObject request = JSONObject.parseObject(plain);
				
				StringBuilder waitSignData = new StringBuilder();
				
				waitSignData.append(request.getString("OrderNo"));
				waitSignData.append(request.getString("PayNo"));
				waitSignData.append(request.getString("PayAmount"));
				waitSignData.append(request.getString("CurrCode"));
				waitSignData.append(request.getString("SystemSSN"));
				waitSignData.append(request.getString("RespCode"));
				waitSignData.append(request.getString("SettDate"));
				waitSignData.append(request.getString("Reserved01"));
				waitSignData.append(request.getString("Reserved02"));
				
				String signature = request.getString("SignMsg");
				
				WriteBankLogLn2("广银联通知返回签名原始数据:\r\n" + waitSignData.toString());
				WriteBankLogLn2("广银联通知返回签名:\r\n" + signature);
				
				// 执行验签
				boolean verifySignResult = verifyNotifyAsyn(waitSignData.toString(), signature, actItem);
				if (verifySignResult) {
					retAts.retHashMap.put("RespCode", "0");
					retAts.retHashMap.put("RespInfo", "验签通过");
				} else {
					retAts.retHashMap.put("RespCode", "-1");
					retAts.retHashMap.put("RespInfo", "验签失败");
				}
			} else {
				retAts.retHashMap.put("RespCode", "-1");
				retAts.retHashMap.put("RespInfo", "未找到账号[" + ownAct + "]商户配置信息");
			}
		} catch (Exception ex) {
			ex.printStackTrace();
			retAts.retHashMap.put("RespCode", "-1");
			retAts.retHashMap.put("RespInfo", ex.getMessage());
			WriteBankLogLn(ex);
		}

		return retAts;
	}
}
